const express = require('express')
const app = express()
var morgan = require('morgan')
var fs = require('fs');
var path = require('path');
morgan.format('joke', '[joke] :method :url :status');
app.use(morgan('joke'));
app.use(express.static('public'));
app.all("*",function(req,res,next){
  //设置允许跨域的域名，*代表允许任意域名跨域
  res.header("Access-Control-Allow-Origin","http://localhost:8081");
  //允许的header类型
  res.header("Access-Control-Allow-Headers","content-type");
  //跨域允许的请求方式
  res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
  if (req.method.toLowerCase() == 'options')
    res.send(200);  //让options尝试请求快速结束
  else
    next();
});

// const winston = require('winston');
 
// logger.log('info', 'Hello distributed log files!');
// logger.info('Hello again distributed logs');

// app.use(logger)
// app.get('/',(req, res, next) => {
//     const obj = {
//         name:1,
//         age:18
//     }
//     res.send(obj)
//   })
//   let obj = {
//       name:1321,
//       age:13
//   }
//   const muban=`<link rel="stylesheet" href="/css/style.css" />
//   <h1>找不到你要的页面了！</h1>
//   <p>你所访问的路径 {{ url }} 不存在</p>`
//   app.get('/list',(req,res)=>{
//         res.send(obj)
//   })
//   app.use('*', (req, res) => {
//     res.status(404).render('404', { url: muban });
//   });
  
//   app.use((err, req, res, next) => {
//     console.error(err.stack);
//     res.status(404).render('404', { url: muban });

//   });
  const indexRouter = require('./router/index');
const apiRouter = require('./router/api');
app.use('/', indexRouter);
app.use('/api', apiRouter);
const host = 8083
app.listen(host)

console.log(`app started at ${host}`)
